package com.example.demo.zaqizabatest;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

/**
 * @ClassName AnalysisJson
 * @Author lys4134
 * @Date 2022/01/13 14:15:26
 * @Description AnalysisJson
 * @Version 1.0
 **/
public class AnalysisJson {

    public static void main(String[] args) throws IOException {

        ObjectMapper objectMapper = new ObjectMapper();
        File file = new File("D:\\20220113北七家310硬盘.json");
        List<VolumeDetailResponse> list = objectMapper.readValue(file, new TypeReference<List<VolumeDetailResponse>>() {
        });
        //模板路径
        String path = "D:\\20220113北七家310硬盘.xlsx";
        SXSSFWorkbook workboot = new SXSSFWorkbook();
        //sheet页（默认sheet1页），也可通过workboot.createSheet("订单列表")创建sheet页
        Sheet sheetAt = workboot.createSheet("Sheet1");


        //开始创建行和表格并且加入数据
        //行
        Row row;
        row = sheetAt.createRow(0);
        row.createCell(0).setCellValue("硬盘cmp上面的id");
        row.createCell(1).setCellValue("硬盘名");
        row.createCell(2).setCellValue("硬盘所挂载主机id");
        row.createCell(3).setCellValue("硬盘所挂载主机名");
        int i = 0;
        if (null != list && !list.isEmpty()) {
            for (VolumeDetailResponse volume : list) {
                if (!StringUtils.isEmpty(volume.getInstanceId()) && !volume.getName().endsWith("_system")) {
                    i += 1;
                    row = sheetAt.createRow(i);
                    row.createCell(0).setCellValue(volume.getId());
                    row.createCell(1).setCellValue(volume.getName());
                    row.createCell(2).setCellValue(volume.getInstanceId());
                    row.createCell(3).setCellValue(volume.getInstanceName());
                }
            }
            try {
                File excel = new File(path);
                if (!excel.getParentFile().exists()) {
                    excel.getParentFile().mkdirs();
                }
                FileOutputStream fileOutputStream = new FileOutputStream(excel);
                workboot.write(fileOutputStream);
            } catch (IOException e) {
                System.out.println( e.getMessage());
            }
        }

    }
}

